//
//  XJHomeHeaderView.m
//  XinJiangMall
//
//  Created by Tzyang on 2018/4/9.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJHomeHeaderView.h"
#import <UIImageView+WebCache.h>
#import "BannerListModel.h" // 广告栏 model

@interface XJHomeHeaderView ()<NewPagedFlowViewDelegate, NewPagedFlowViewDataSource>

@end

@implementation XJHomeHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self= [super initWithFrame:frame]) {
        self.backgroundColor = WHITE_COLOR;
        [self setupSubviews];
    }
    return self;
    
}

- (NewPagedFlowView *)mainView {
    
    if (!_mainView) {
        NewPagedFlowView * mainView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, ADAPTHeight(5), KWidth, ADAPTHeight(165))];
        self.mainView = mainView;
        [self addSubview:mainView];
    }
    return _mainView;
}



- (void)setupSubviews{
    
    self.mainView.minimumPageAlpha = 0.1;
    self.mainView.isCarousel = YES ;
    self.mainView.orientation = NewPagedFlowViewOrientationHorizontal;
    self.mainView.isOpenAutoScroll = YES;
    
    self.mainView.delegate = self;
    self.mainView.dataSource = self;
    
    //初始化pageControl
    UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.mainView.frame.size.height - 32, KWidth, 8)];
    self.mainView.pageControl = pageControl;
    [self.mainView addSubview:pageControl];
    
    [self addSubview:self.mainView];
    
}

#pragma mark 轮播图 Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    return CGSizeMake(KWidth - 50, ADAPTHeight(165));
}

- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    
    if (self.didSelectImgBlock) {
        self.didSelectImgBlock(self.bannerArray[subIndex]);
    }
    
    XJLog(@"点击了第%ld张banner图",(long)subIndex + 1);
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    
    //XJLog(@"ViewController 滚动到了第%ld页",pageNumber);
}

#pragma mark 轮播图 Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    
    return _bannerArray.count;
    
}

- (PGIndexBannerSubiew *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew *bannerView = [flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[PGIndexBannerSubiew alloc] init];
        bannerView.tag = index;
        bannerView.layer.cornerRadius = 10;
        bannerView.layer.masksToBounds = YES;
    }
    
    // 取 `BannerListModel` 里面的图片url字段
    BannerListModel *bannerListM = _bannerArray[index];
    NSString *imgUrlStr = bannerListM.image;
    
    [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:imgUrlStr] placeholderImage:[UIImage imageNamed:@"default_banner"]];

    //在这里下载网络图片
//    [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:_bannerArray[index]] placeholderImage:[UIImage imageNamed:@"default_banner"]];
    //bannerView.mainImageView.image = self.imgDataArray[index];
    
    return bannerView;
}


- (void)setBannerArray:(NSArray *)bannerArray{
    
    _bannerArray = bannerArray;
//    XJLog(@"bannerArray = %@",bannerArray);
    [self.mainView reloadData];
}


//- (void)setTagsArray:(NSArray *)tagsArray{
//    
//    _tagsArray  = tagsArray;
//    XJLog(@" tagsArray = > %@",tagsArray);
//    [tagsArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        UIButton * homeTagBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        
//        NSUInteger index = idx % 5;
//        NSUInteger page = idx / 5;
//        
//        homeTagBtn.frame = CGRectMake(ADAPTWidth(30)+ index * (ADAPTWidth(48)+ADAPTWidth(20)),page * (ADAPTHeight(48)+ADAPTHeight(40))+ CGRectGetMaxY(self.mainView.frame)+ADAPTHeight(20), ADAPTWidth(48),ADAPTHeight(48));
//        
//        homeTagBtn.tag = idx;
//        [homeTagBtn buttonWithTitle:[obj lastObject] titleFont:XJFONTSET_R(12) titleColor:XJCOLOR_RGB(17, 17, 17) imageType:ImageTypeUrl imageName:[obj firstObject] withTarget:self action:@selector(homeTagBtnClick:) style:ImgTextBtnStyleTop margin:180];
//        [self addSubview:homeTagBtn];
//    }];
//    
//}
//
//
//- (void)homeTagBtnClick:(UIButton *)btn{
//    
//    XJLog(@"tag = > %ld",(long)btn.tag);
//    
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
